<?php
class User extends MY_Controller {
    public function __construct() {
        parent::__construct();
        $this->load->model('Muser');
    }
    
    public function index() {
        //Pre-load View
        $this->_data['load_page'] = 'user/index_view';        
        if ($this->input->post('yt0')) {
            $user = $this->input->post('User');
            //Form validation
            $this->load->library('form_validation');
            $this->form_validation->set_rules('User[name]', 'User name', 'required');
            $this->form_validation->set_rules('User[birth_date]', 'Birth Date', 'required');
            $this->form_validation->set_rules('User[login]', 'Login', 'required');
            $this->form_validation->set_rules('User[email]', 'Email', 'required|valid_email');
            if ($this->form_validation->run()) {                 		            
                $this->Muser->insertUser($user);
                $this->session->set_flashdata('flash_mess', 'User has been created successfully.'); 
                redirect(base_url('user/manage'));

            } else {
                $this->_data['data']['error'] = validation_errors('<li>', '</li>');
            }
        }
        //Load View
        //$this->_data['data']['flash_mess'] = $this->session->flashdata('flash_mess');
        $this->load->view($this->_data['template'], $this->_data);
    }
    
    public function display($page="0") {
        $this->_data['load_page'] = 'user/display_view';
        //Pagination   
        $config['base_url'] = base_url('user/display/');
        $config['total_rows'] = $this->Muser->countAll();
        $config['per_page'] = 10; 
        $this->load->library('pagination', $config);
        $this->_data['pagination_links'] = $this->pagination->create_links();
        //Load View
        $this->_data['data']['user'] = $this->Muser->listAllUsers($config['per_page'], $page, 'id desc');  
        $this->load->view($this->_data['template'], $this->_data);
    }
    
    public function manage($page="0") {
        //Pre-load View
        $this->_data['load_page'] = 'user/manage_view'; 
        //Pagination   
        $config['base_url'] = base_url('user/manage/');
        $config['total_rows'] = $this->Muser->countAll();
        $config['per_page'] = 10; 
        $this->load->library('pagination', $config);
        $this->_data['pagination_links'] = $this->pagination->create_links();
        //Load View
        //echo '<pre>';
        //print_r($user);echo'</pre>';exit();
        $this->_data['current_page'] = $page;
        $this->_data['data']['user'] = $this->Muser->listAllUsers($config['per_page'], $page, 'id desc');  
        $this->_data['flash_mess'] = $this->session->flashdata('flash_mess');  
        $this->load->view($this->_data['template'], $this->_data);
    }
    
    public function update($id) {
        //Pre-load View
        $this->_data['load_page']= 'user/update_view';
        //Process
        if ($this->input->post('yt0')) {
            //Form Validation
            $this->load->library('form_validation');
            $this->form_validation->set_rules('User[name]', 'User name', 'required');
            $this->form_validation->set_rules('User[birth_date]', 'Birth Date', 'required');
            $this->form_validation->set_rules('User[login]', 'Login', 'required');
            $this->form_validation->set_rules('User[email]', 'Email', 'required|valid_email');
            if ($this->form_validation->run()) {
                $data_update = $this->input->post('User');
                if ($data_update['password'] != '') {
                    $data_update['password'] = md5($data_update['password']);
                } else {
                    unset($data_update['password']);
                }
                $this->Muser->updateUser($data_update, $id);
                $this->session->set_flashdata('flash_mess', 'User has been created successfully.'); 
                redirect(base_url('user/manage'));
            } else {
                $this->_data['data']['error'] = validation_errors('<li>','</li>');
            }  
        }
        //Load View
        $this->_data['data'] = $this->Muser->getUserById($id);
        $this->_data['data_id'] = $id;
        $this->load->view($this->_data['template'], $this->_data);
    }
    
    public function delete($id) {
        $this->Muser->deleteUser($id);
        redirect(base_url('user/manage'));
    }
    
    public function check_name_exist() {
        $input = $this->input->post("User");
        if ($this->Muser->getUserByName($input['name']) != 0) {
            $this->form_validation->set_message('check_name_exist', 'Name has been created in system. Please choose another');
            return false;
        } else {
            return true;
        }
    }
}